package com.itheima.solr.test;

import com.itheima.solr.pojo.Item;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

import java.io.IOException;

public class SolrTestCreate {
    /**
     * 以Document形式添加或修改数据
     * @throws IOException
     * @throws SolrServerException
     */
    @Test
    public void test1() throws IOException, SolrServerException {

        System.out.println("helloword2");
        //连接solr服务器


        System.out.println("helloworld");
        HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8080/solr/test");
        //创建solr输入document
        SolrInputDocument document = new SolrInputDocument();
        //添加字段
        document.addField("id", "7");
        document.addField("title","现代汽车，韩国棒子造");
        document.addField("price",999);
        httpSolrServer.add(document);
        //4.提交请求
        //若id存在，则新增
        //若id不存在，则修改
        httpSolrServer.commit();

    }
    @Test
    public void test2() throws IOException, SolrServerException {
        //连接solr服务器
        HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8080/solr/test");
        //创建solr输入document
        Item item = new Item();
        item.setId("4");
        item.setTitle("xiaomi,不仅仅是世界500强");
        item.setPrice(99999l);
        //添加字段
        httpSolrServer.addBean(item);
        httpSolrServer.commit();

    }
}
